package com.freewater.springboot.demo.validation.baseuse;

import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import jakarta.validation.metadata.ConstraintDescriptor;
import org.hibernate.validator.internal.engine.constraintvalidation.ConstraintValidatorContextImpl;

import java.lang.annotation.Annotation;
import java.util.Collection;

public class ListGtValidator implements ConstraintValidator<ListGt, Collection<Object>> {

    @Override
    public void initialize(ListGt constraintAnnotation) {
        ConstraintValidator.super.initialize(constraintAnnotation);
    }

    @Override
    public boolean isValid(Collection value, ConstraintValidatorContext context) {
        if (value == null) {
            return true;
        }
        ConstraintValidatorContextImpl constraintValidatorContext = (ConstraintValidatorContextImpl) context;
        final ConstraintDescriptor<?> constraintDescriptor = constraintValidatorContext.getConstraintDescriptor();
        final Annotation annotation = constraintDescriptor.getAnnotation();
        ListGt listGt = (ListGt) annotation;
        final long minValue = listGt.value();
        return value.size() >= minValue;
    }

}
